package com.linkai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.linkai.dto.AdminBproductExcelDto;
import com.linkai.dto.AdminBproductGetDto;
import com.linkai.dto.AdminBproductIndexDto;
import com.linkai.entity.*;
import com.linkai.mapper.*;
import com.linkai.service.BproductService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yamon
 * @since 2021-02-19
 */
@Service
public class BproductServiceImpl extends ServiceImpl<BproductMapper, Bproduct> implements BproductService {
    @Autowired
    private BproductMapper bproductMapper;
    @Autowired
    private BoriginplaceMapper boriginplaceMapper;

    @Autowired
    private SizeMapper sizeMapper;

    @Autowired
    private BproSizeMapper bproSizeMapper;

    @Autowired
    private BstoreMapper bstoreMapper;

    @Autowired
    private BcategoryMapper bcategoryMapper;

    @Autowired
    private ColorMapper colorMapper;


    /**
     * 是否存在一条数据
     *
     * @return flag
     */
    @Override
    public boolean isExist() {
        final QueryWrapper<Bproduct> queryWrapper = new QueryWrapper<>();
        final Integer count = bproductMapper.selectCount(queryWrapper);
        return count != 0;
    }

    /**
     * 查找所有商品
     *
     * @return list
     */
    @Override
    public List<AdminBproductIndexDto> listAllProduct4AdminIndex() {
        return bproductMapper.selectAllProduct4AdminIndex();
    }

    /**
     * 从excel导入数据到数据库中
     *
     * @param bproductDtos dto
     * @return 数目
     */
    @Override
    public int batchSaveBackProduct(List<AdminBproductExcelDto> bproductDtos) {
        //先存储外键
        for (AdminBproductExcelDto bproductDto : bproductDtos) {
            final Bproduct product = new Bproduct();
            //产地
            final String orig = bproductDto.getOrig();
            //先保存产地信息
            if (orig!=null){
                //找出来产地的信息
                final QueryWrapper<Boriginplace> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("borig_title",orig);
                final Boriginplace boriginplace = boriginplaceMapper.selectOne(queryWrapper);
                //没有查到，新增一个
                if (boriginplace==null){
                    //新增
                    final Boriginplace saveOri = new Boriginplace();
                    saveOri.setBorigTitle(orig);
                    boriginplaceMapper.insert(saveOri);
                    product.setBorigId(saveOri.getBorigId());
                }else{
                    product.setBorigId(boriginplace.getBorigId());
                }
            }
            //库存信息
            final String sto = bproductDto.getSto();
            if (sto!=null){
                //不是null，找到库存信息保存到product表中
                final QueryWrapper<Bstore> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("bsto_title",sto);
                final Bstore bstore = bstoreMapper.selectOne(queryWrapper);
                if (bstore==null){
                    //保存一个新的
                    final Bstore saveBstore = new Bstore();
                    saveBstore.setBstoTitle(sto);
                    bstoreMapper.insert(saveBstore);
                    product.setBstoId(saveBstore.getBstoId());
                }else {
                    //证明已经有
                    product.setBstoId(bstore.getBstoId());
                }
            }
            //类别
            final String category = bproductDto.getCategory();
            if (category!=null){
                //查找一个
                final QueryWrapper<Bcategory> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("bcat_desc",category);
                final Bcategory bcategory = bcategoryMapper.selectOne(queryWrapper);
                if (bcategory==null){
                    //新建一个
                    final Bcategory saveCategory = new Bcategory();
                    saveCategory.setBcatDesc(category);
                    bcategoryMapper.insert(saveCategory);
                    product.setBcatId(saveCategory.getBcatId());
                }else{
                    product.setBcatId(bcategory.getBcatId());
                }
            }
            product.setBproTitle(bproductDto.getProTitle());
            product.setBproDesc(bproductDto.getProDesc());
            product.setBproUnit(bproductDto.getProUnit());
            product.setBinputPrice(bproductDto.getInputPrice());
            product.setBoutputPrice(bproductDto.getOutputPrice());
            product.setBpredictPrice(bproductDto.getPredictPrice());
            product.setBproNum(bproductDto.getProNum());
            product.setBbackup(bproductDto.getBackup());
            //保存商品细腻些
            bproductMapper.insert(product);
            //尺寸
            final List<String> sizes = Arrays.asList(bproductDto.getSize().split(","));
            for (String size : sizes) {
                //遍历size，查找size尺寸，标准匹配
                final QueryWrapper<Size> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("bsize_desc",size);
                final Size bsize = sizeMapper.selectOne(queryWrapper);
                if (bsize==null){
                    //保存一个新的尺寸信息
                    final Size saveSize = new Size();
                    saveSize.setSizeDesc(size);
                    sizeMapper.insert(saveSize);
                    //保存公共表
                    final BproSize bproSize = new BproSize();
                    bproSize.setBproId(product.getBproId());
                    bproSize.setBsizeId(saveSize.getSizeId());
                    bproSizeMapper.insert(bproSize);
                }
            }
        }

        return 0;
    }

    /**
     * 得到一个商品详情信息
     *
     * @param id id
     * @return object
     */
    @Override
    public AdminBproductGetDto getOneAdminBackProduct(Integer id) {
        AdminBproductGetDto adminBproductGetDto = new AdminBproductGetDto();
        //得到所有的信息
        final Bproduct bproduct = baseMapper.selectById(id);
        BeanUtils.copyProperties(bproduct,adminBproductGetDto);

        //商品分类
        final Integer bcatId = bproduct.getBcatId();
        final Bcategory bcategory = bcategoryMapper.selectById(bcatId);
        adminBproductGetDto.setBcategory(bcategory);
        //产地
        final Integer borigId = bproduct.getBorigId();
        final Boriginplace boriginplace = boriginplaceMapper.selectById(borigId);
        adminBproductGetDto.setBoriginplace(boriginplace);
        //尺寸
        adminBproductGetDto.setSizeName(sizeMapper.selectById(bproduct.getBsizeId()).getSizeDesc());
        //颜色
        adminBproductGetDto.setColorName(colorMapper.selectById(bproduct.getColorId()).getColDesc());
        //所属仓库
        final Integer bstoId = bproduct.getBstoId();
        final Bstore bstore = bstoreMapper.selectById(bstoId);
        adminBproductGetDto.setBstore(bstore);
        return adminBproductGetDto;
    }
}
